package com.fingard.dsp.bank.directbank.jyrb01;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/4/24.
 */
public class JYRB01SinglePay extends JYRB01Base implements ISinglePay {

    public JYRB01SinglePay() {
    }

    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAtsSingle  =new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try {
            JYRB01Header JYRB01Header = new JYRB01Header();
            JYRB01Header.trsCode = "B2ETransferHx";
            JYRB01Header.ERPTrsTimestamp = p_req1908.reqHeader.transTime;
            JYRB01Header.ERPJnlNo = p_req1908.reqHeader.reqSeqID;
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            wirteCommenHeader(xmlTextWriter, JYRB01Header);
            xmlTextWriter.writeStartElement("Body");
            xmlTextWriter.writeStartElement("List");
            XElement tmpXElement = new XElement("Map","");
            tmpXElement.addChild(new XElement("TransferType","1".equals(p_req1908.singleDetail.sameBnk)?"0":"1"));//转账类型
            tmpXElement.addChild(new XElement("PayerAcNo",p_req1908.singleDetail.payAct));//转出账号
            tmpXElement.addChild(new XElement("Amount",p_req1908.singleDetail.payAmount));//金额
            tmpXElement.addChild(new XElement("PayeeAcNo",p_req1908.singleDetail.recAct));//转入账号
            tmpXElement.addChild(new XElement("PayeeAcName",p_req1908.singleDetail.recName));//转入户名
            tmpXElement.addChild(new XElement("UnionDeptId",p_req1908.singleDetail.cnapsCode));//转入行清算行号行号
            tmpXElement.addChild(new XElement("DrecCode",p_req1908.singleDetail.cnapsCode));//转入行行号
            tmpXElement.addChild(new XElement("Currency",convertCurToBank(p_req1908.singleDetail.payCur)));//转入行行号
            tmpXElement.addChild(new XElement("Remark",p_req1908.singleDetail.purpose));//转账用途
            tmpXElement.addChild(new XElement("Priority","0"));//转账方式
            nodeAnalysis(p_req1908.singleDetail.srcXElement,tmpXElement,getCharset());
            xmlTextWriter.writeXElement(tmpXElement);
            xmlTextWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlTextWriter.toXmlString(), JYRB01Header.trsCode);
            if(bankResult[0].length()==0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader xmlTextReader = loadBankRespXmlReader(bankResult[1],tmpSbMsg);
                if(xmlTextReader!=null){
                    String IBSReturnCode = xmlTextReader.getFirstText("/Message/Head/IBSReturnCode");
                    String IBSJnlNo = xmlTextReader.getFirstText("/Message/Head/IBSJnlNo");
                    if("000000".equals(IBSReturnCode)){
                        String returnCode = xmlTextReader.getFirstText("/Message/Body/List/Map/ReturnCode");
                        String returnMsg = xmlTextReader.getFirstText("/Message/Body/List/Map/ReturnMsg");
                            detail.respCode  ="0";
                            detail.respInfo  = returnMsg;
                            detail.reqSeqID = IBSJnlNo;
                            detail.transState = returnCode;
                            detail.payInfoCode = detail.transState;
                    }else{
                        String IBSReturnMsg = xmlTextReader.getFirstText("/Message/Body/IBSReturnMsg");
                        detail.respCode  ="0";
                        detail.respInfo  = IBSReturnMsg;
                        detail.reqSeqID = IBSJnlNo;
                        detail.transState = IBSReturnCode;
                        detail.payInfoCode = IBSReturnCode;
                    }
                }else{
                    detail.respCode  ="-1";
                    detail.respInfo  = tmpSbMsg.toString();
                }
            }else if (bankResult[0].equals(ConnState.FAIL)) {
                detail.respCode = "0";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
                detail.transState = PaymentState.FAIL_Payment;
                detail.payInfoCode = bankResult[0];
            }else {
                detail.respCode  ="-1";
                detail.respInfo  = bankResult[1];
            }
        }catch (Exception ex){
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
            ex.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        retAtsSingle.writeEnd();
        return retAtsSingle;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try {
            JYRB01Header JYRB01Header = new JYRB01Header();
            JYRB01Header.trsCode = "B2ETransferQryHx";
            JYRB01Header.ERPTrsTimestamp = p_req1944.reqHeader.transTime;
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            wirteCommenHeader(xmlTextWriter, JYRB01Header);
            xmlTextWriter.writeStartElement("Body");
            xmlTextWriter.writeElementString("HistoryERPJnlNo",p_req1944.askNumber);
            xmlTextWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlTextWriter.toXmlString(), JYRB01Header.trsCode);
            if(bankResult[0].length()==0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpSbMsg);
                if(tmpBkReader!=null){
                    String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                    if("000000".equals(IBSReturnCode)){
                        String returnCode = tmpBkReader.getFirstText("/Message/Body/List/Map/ResCode");
                        String returnMsg =tmpBkReader.getFirstText("/Message/Body/List/Map/ResMsg").split("@")[0];
                        detail.respCode = "0";
                        detail.respInfo = returnMsg;
                        detail.reqSeqID = p_req1944.askNumber;
                        detail.transState = returnCode;
                        detail.payInfoCode = returnCode;
                    }else{
                        String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        detail.respCode = "0";
                        detail.respInfo = IBSReturnMsg;
                        detail.reqSeqID = p_req1944.askNumber;
                        detail.transState = IBSReturnCode;
                        detail.payInfoCode = IBSReturnCode;
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            }else{
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception ex){
            detail.respCode = "-1";
            detail.respInfo = ex.getMessage();
            ex.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
